package net.caixiaomi.info.ui.football;

import android.content.Intent;
import android.os.Bundle;
import android.support.annotation.Nullable;
import android.support.v4.app.FragmentManager;
import android.support.v4.app.FragmentTransaction;
import android.support.v7.widget.LinearLayoutManager;
import android.text.TextUtils;
import android.view.LayoutInflater;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.PopupWindow;
import android.widget.TextView;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import com.google.gson.reflect.TypeToken;

import net.caixiaomi.info.R;
import net.caixiaomi.info.WebViewActivity;
import net.caixiaomi.info.app.CommonApp;
import net.caixiaomi.info.base.BaseActivity;
import net.caixiaomi.info.base.quickadapter.entity.MultiItemEntity;
import net.caixiaomi.info.helper.AppHelper;
import net.caixiaomi.info.model.BaseCallModel;
import net.caixiaomi.info.model.CommonEvent;
import net.caixiaomi.info.model.MatchItem;
import net.caixiaomi.info.net.RequestService;
import net.caixiaomi.info.net.ResponseCallback;
import net.caixiaomi.info.net.ResponseError;
import net.caixiaomi.info.net.RetrofitManage;
import net.caixiaomi.info.ui.dialog.MatchFilterDialogFragment;
import net.caixiaomi.info.util.Constants;
import net.caixiaomi.info.util.EventWhat;
import net.caixiaomi.info.util.ToastUtil;

import java.util.ArrayList;
import java.util.List;

import butterknife.BindView;
import retrofit2.Call;

import static com.alibaba.fastjson.JSON.parseObject;

/**
 * Created by suntian on 2018/8/21.
 */

public class FootballNewActivity extends BaseActivity{

    private int mCurrentPosition=0;

    @BindView(R.id.lottery_select_arrow)
    ImageView mSelectArrow;

    @BindView(R.id.lottery_select_group)
    LinearLayout mSelectGroup;

    private PopupWindow mSelectPopup;
    private View mSelectView;

    private int mType = Constants.TYPE_VICTORY_AND_DEFEAT;

    PlayFootballFragment fragment0;
    PlayFootballFragment fragment1;
    PlayFootballFragment fragment2;
    PlayFootballFragment fragment3;
    PlayFootballFragment fragment4;
    PlayFootballFragment fragment5;
    PlayFootballFragment fragment6;

    @Override
    protected int layout() {
        return R.layout.football_main_layout;
    }

    @Override
    protected void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        mType= Constants.TYPE_BLEND;

        final View barView=findViewById(R.id.toolbar);
        mSelectGroup.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                showListPopupWindow(barView);
            }
        });
        mSelectView=View.inflate(this,R.layout.football_select_layout,null);
        manager=getSupportFragmentManager();

        initSelectView();
        mSelectView.findViewById(R.id.select0_lottery_left).performClick();
    }

    public void showListPopupWindow(View view) {
        if (mSelectPopup == null) {
            mSelectPopup = new PopupWindow(this);
        }

        mSelectPopup.setFocusable(true);
        if(mSelectPopup.isShowing()){
            mSelectPopup.dismiss();
            return;
        }

        mSelectPopup.setContentView(mSelectView);

        mSelectPopup.setOnDismissListener(new PopupWindow.OnDismissListener() {
            @Override
            public void onDismiss() {
                //旋转0度是复位ImageView
                mSelectArrow.animate().setDuration(300).rotation(0).start();
            }
        });

        mSelectPopup.setOutsideTouchable(true);
        mSelectPopup.setWidth(CommonApp.mScreenWidth);
        mSelectPopup.setBackgroundDrawable(null);
        mSelectPopup.showAsDropDown(view);
        mSelectArrow.animate().setDuration(300).rotation(180).start();
    }

    private List<TextView> mSelectTvList;
    private List<ImageView> mSelectImgList;
    private FragmentManager manager;

    View.OnClickListener popupListener=new View.OnClickListener() {
        @Override
        public void onClick(View view) {
            clearSelectType();
            switch (view.getId()){
                case R.id.select0_lottery_left:
                    mType=Constants.TYPE_BLEND;
                    mSelectTvList.get(0).setTextColor(getResources().getColor(R.color.orange_primary));
                    mSelectImgList.get(0).setVisibility(View.VISIBLE);
                    setSelection(0);
                    break;
                case R.id.select0_lottery_mid:
                    mType=Constants.TYPE_VICTORY_AND_DEFEAT;
                    mSelectTvList.get(1).setTextColor(getResources().getColor(R.color.orange_primary));
                    mSelectImgList.get(1).setVisibility(View.VISIBLE);
                    setSelection(1);
                    break;
                case R.id.select0_lottery_right:
                    mType=Constants.TYPE_VICTORY_AND_DEFEAT_1;
                    mSelectTvList.get(2).setTextColor(getResources().getColor(R.color.orange_primary));
                    mSelectImgList.get(2).setVisibility(View.VISIBLE);
                    setSelection(2);
                    break;
                case R.id.select1_lottery_left:
                    mType=Constants.TYPE_ALL_GOALS;
                    mSelectTvList.get(3).setTextColor(getResources().getColor(R.color.orange_primary));
                    mSelectImgList.get(3).setVisibility(View.VISIBLE);
                    setSelection(3);
                    break;
                case R.id.select1_lottery_mid:
                    mType=Constants.TYPE_HALF_AND_ALL;
                    mSelectTvList.get(4).setTextColor(getResources().getColor(R.color.orange_primary));
                    mSelectImgList.get(4).setVisibility(View.VISIBLE);
                    setSelection(4);
                    break;
                case R.id.select1_lottery_right:
                    mType=Constants.TYPE_SCORE;
                    mSelectTvList.get(5).setTextColor(getResources().getColor(R.color.orange_primary));
                    mSelectImgList.get(5).setVisibility(View.VISIBLE);
                    setSelection(5);
                    break;
                case R.id.select2_lottery_left:
                    mType=Constants.TYPE_2_IN_1;
                    mSelectTvList.get(6).setTextColor(getResources().getColor(R.color.orange_primary));
                    mSelectImgList.get(6).setVisibility(View.VISIBLE);
                    setSelection(6);
                    break;
            }
            if(mSelectPopup!=null) {
                mSelectPopup.dismiss();
            }

            String title = getTitleFormType();
            TextView titleTv= (TextView) findViewById(R.id.title);
            titleTv.setText(getString(R.string.app_name)+" · "+title);
        }
    };

//    public boolean onCreateOptionsMenu(Menu menu) {
//        getMenuInflater().inflate(R.menu.play_football, menu);
//        return true;
//    }

    private void initSelectView(){
        mSelectTvList=new ArrayList<>();
        mSelectImgList=new ArrayList<>();
        mSelectView.findViewById(R.id.select0_lottery_left).setOnClickListener(popupListener);
        TextView tv0=mSelectView.findViewById(R.id.select0_lottery_left_tv);
        ImageView iv0=mSelectView.findViewById(R.id.select0_lottery_left_img);
        mSelectTvList.add(tv0);
        mSelectImgList.add(iv0);

        mSelectView.findViewById(R.id.select0_lottery_mid).setOnClickListener(popupListener);
        TextView tv1=mSelectView.findViewById(R.id.select0_lottery_mid_tv);
        ImageView iv1=mSelectView.findViewById(R.id.select0_lottery_mid_img);
        mSelectTvList.add(tv1);
        mSelectImgList.add(iv1);

        mSelectView.findViewById(R.id.select0_lottery_right).setOnClickListener(popupListener);
        TextView tv2=mSelectView.findViewById(R.id.select0_lottery_right_tv);
        ImageView iv2=mSelectView.findViewById(R.id.select0_lottery_right_img);
        mSelectTvList.add(tv2);
        mSelectImgList.add(iv2);

        mSelectView.findViewById(R.id.select1_lottery_left).setOnClickListener(popupListener);
        TextView tv3=mSelectView.findViewById(R.id.select1_lottery_left_tv);
        ImageView iv3=mSelectView.findViewById(R.id.select1_lottery_left_img);
        mSelectTvList.add(tv3);
        mSelectImgList.add(iv3);

        mSelectView.findViewById(R.id.select1_lottery_mid).setOnClickListener(popupListener);
        TextView tv4=mSelectView.findViewById(R.id.select1_lottery_mid_tv);
        ImageView iv4=mSelectView.findViewById(R.id.select1_lottery_mid_img);
        mSelectTvList.add(tv4);
        mSelectImgList.add(iv4);

        mSelectView.findViewById(R.id.select1_lottery_right).setOnClickListener(popupListener);
        TextView tv5=mSelectView.findViewById(R.id.select1_lottery_right_tv);
        ImageView iv5=mSelectView.findViewById(R.id.select1_lottery_right_img);
        mSelectTvList.add(tv5);
        mSelectImgList.add(iv5);

        mSelectView.findViewById(R.id.select2_lottery_left).setOnClickListener(popupListener);
        TextView tv6=mSelectView.findViewById(R.id.select2_lottery_left_tv);
        ImageView iv6=mSelectView.findViewById(R.id.select2_lottery_left_img);
        mSelectTvList.add(tv6);
        mSelectImgList.add(iv6);
    }

    public boolean onOptionsItemSelected(MenuItem item) {
        int id = item.getItemId();
        switch (id) {
            case R.id.filter:
                AppHelper.sendStatistics("shaixuansszc", new String[]{getString(R.string.C_TYPE_OF_LOTTERY)}, new String[]{getTitleFormType()});
                switch (mCurrentPosition){
                    case 0:
                        fragment0.showMatchFilterDialog();
                        break;
                    case 1:
                        fragment1.showMatchFilterDialog();
                        break;
                    case 2:
                        fragment2.showMatchFilterDialog();
                        break;
                    case 3:
                        fragment3.showMatchFilterDialog();
                        break;
                    case 4:
                        fragment4.showMatchFilterDialog();
                        break;
                    case 5:
                        fragment5.showMatchFilterDialog();
                        break;
                    case 6:
                        fragment6.showMatchFilterDialog();
                        break;
                }
                break;
            case R.id.btn_help:
                AppHelper.sendStatistics("saishibangzhuzc", new String[]{getString(R.string.C_TYPE_OF_LOTTERY)}, new String[]{getTitleFormType()});
                StringBuilder stringBuilder = new StringBuilder();
                stringBuilder.append(Constants.BASE_H5_URL).append(getString(R.string.H5_PLAY_INTRODUCE));
                String url = stringBuilder.toString();
                Intent intent = new Intent(this, WebViewActivity.class);
                intent.putExtra("url", url);
                startActivity(intent);
                break;

        }
        return true;
    }

    private void clearSelectType(){
        for(int i=0;i<mSelectTvList.size();i++){
            mSelectTvList.get(i).setTextColor(getResources().getColor(R.color.primary_text));
            mSelectImgList.get(i).setVisibility(View.INVISIBLE);
        }
    }

    public void setSelection(int position) {
        mCurrentPosition=position;
        FragmentTransaction transaction = manager.beginTransaction();
        hideFragments(transaction);

        switch (position){
            case 0:
                if(fragment0==null){
                    fragment0=new PlayFootballFragment();
                    transaction.add(R.id.mmfragment, fragment0);
                    fragment0.setType(Constants.TYPE_BLEND);
                }

                transaction.show(fragment0);
                break;
            case 1:
                if(fragment1==null){
                    fragment1=new PlayFootballFragment();
                    transaction.add(R.id.mmfragment, fragment1);
                    fragment1.setType(Constants.TYPE_VICTORY_AND_DEFEAT);

                }
                transaction.show(fragment1);
                break;
            case 2:
                if(fragment2==null){
                    fragment2=new PlayFootballFragment();
                    transaction.add(R.id.mmfragment, fragment2);
                    fragment2.setType(Constants.TYPE_VICTORY_AND_DEFEAT_1);

                }
                transaction.show(fragment2);
                break;
            case 3:
                if(fragment3==null){
                    fragment3=new PlayFootballFragment();
                    transaction.add(R.id.mmfragment, fragment3);
                    fragment3.setType(Constants.TYPE_ALL_GOALS);

                }
                transaction.show(fragment3);
                break;
            case 4:
                if(fragment4==null){
                    fragment4=new PlayFootballFragment();
                    transaction.add(R.id.mmfragment, fragment4);
                    fragment4.setType(Constants.TYPE_HALF_AND_ALL);

                }
                transaction.show(fragment4);
                break;
            case 5:
                if(fragment5==null){
                    fragment5=new PlayFootballFragment();
                    transaction.add(R.id.mmfragment, fragment5);
                    fragment5.setType(Constants.TYPE_SCORE);

                }
                transaction.show(fragment5);
                break;
            case 6:
                if(fragment6==null){
                    fragment6=new PlayFootballFragment();
                    transaction.add(R.id.mmfragment, fragment6);
                    fragment6.setType(Constants.TYPE_2_IN_1);

                }
                transaction.show(fragment6);
                break;
        }
        transaction.commit();
    }

    private void hideFragments(FragmentTransaction transaction) {

        if(fragment0!=null) {
            transaction.hide(fragment0);
        }
        if(fragment1!=null) {
            transaction.hide(fragment1);
        }
        if(fragment2!=null) {
            transaction.hide(fragment2);
        }
        if(fragment3!=null) {
            transaction.hide(fragment3);
        }
        if(fragment4!=null) {
            transaction.hide(fragment4);
        }
        if(fragment5!=null) {
            transaction.hide(fragment5);
        }
        if(fragment6!=null) {
            transaction.hide(fragment6);
        }
    }

    private String getTitleFormType() {
        String title = "";
        switch (mType) {
            case Constants.TYPE_VICTORY_AND_DEFEAT:
                title = getString(R.string.C_VICTORY_AND_DEFEAT);
                break;
            case Constants.TYPE_VICTORY_AND_DEFEAT_1:
                title = getString(R.string.C_VICTORY_AND_DEFEAT_1);
                break;
            case Constants.TYPE_ALL_GOALS:
                title = getString(R.string.C_ALL_GOALS);
                break;
            case Constants.TYPE_HALF_AND_ALL:
                title = getString(R.string.C_HALF_AND_ALL);
                break;
            case Constants.TYPE_SCORE:
                title = getString(R.string.C_SCORE);
                break;
            case Constants.TYPE_BLEND:
                title = getString(R.string.C_BLEND);
                break;
            case Constants.TYPE_2_IN_1:
                title = getString(R.string.C_2_IN_1);
                break;
        }
        return title;
    }
}
